package org.jsbd.boss.service.impl;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;
import org.jsbd.boss.action.SystemController;
import org.jsbd.boss.dao.ISystemDAO;
import org.jsbd.boss.domian.channel.DaemonApkConfig;
import org.jsbd.boss.domian.channel.FileUploadVo;
import org.jsbd.boss.domian.channel.OS;
import org.jsbd.boss.service.ISystemService;
import org.jsbd.boss.util.DateUtil;
import org.jsbd.boss.util.FileUploader;
import org.jsbd.boss.util.MD5FileUtil;
import org.jsbd.boss.util.OssUtils;
import org.jsbd.boss.util.PropertiesUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import edu.hziee.cap.common.util.Md5Utils;

@Service("systemService")
public class SystemServiceImpl implements ISystemService {

	public static final Logger LOGGER = LoggerFactory.getLogger(SystemServiceImpl.class);

	@Resource
	private ISystemDAO systemDao;

	@Override
	public Map<String, Object> getSystemByPage(OS os) {
		return systemDao.selectSystemByPage(os);
	}

	/*@Override
	@Transactional("transactionManagerCrack")
	public void saveSystem(MultipartFile[] files, OS os) {
		if (files != null && files.length > 0) {
			MultipartFile file = files[0];
			FileUploadVo fileVo = uploadFile(file);
			if (fileVo != null) {
				String fileName = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("/") + 1);
				String fileType = fileName.substring(fileName.lastIndexOf(".") + 1);
				String filePath = fileVo.getFilePath();
				String md5 = fileVo.getMd5();
				if (StringUtils.isNotBlank(fileType)) {
					os.setMark(md5);
					os.setFilePath(filePath);
				}
			}
		}
		systemDao.insertSystem(os);
	}*/
	
	@Override
	@Transactional("transactionManagerCrack")
	public void saveSystem(MultipartFile[] files, OS os) {
		if (files != null && files.length > 0) {
			MultipartFile file = files[0];
			String md5 = "";
			File f;
			try {
				f = File.createTempFile("tmp", null);
				file.transferTo(f);
				md5 = MD5FileUtil.getMD5(f);
				f.deleteOnExit();
			} catch (IOException e) {
				e.printStackTrace();
			}
			
			Runtime r = Runtime.getRuntime();  
			r.gc();
			long startMem = r.freeMemory(); // 开始时的剩余内存  
			String filePath = FileUploader.fileUpload(files[0], md5);
			long orz = startMem - r.freeMemory(); // 剩余内存 现在
			System.out.println("--------------------------"+String.valueOf(orz));
			
			os.setMark(md5);
			os.setFilePath(filePath);
		}
		systemDao.insertSystem(os);
	}

	/*private FileUploadVo uploadFile(MultipartFile file) {
		String fileUploadPath = PropertiesUtil.getValue("game_file_upload_path");
		FileUploadVo fileVo = null;
		if (file != null && !file.isEmpty()) {
			File apkFile = null;
			try {
				fileVo = new FileUploadVo();
				String originalFilename = file.getOriginalFilename();
				String fileName = originalFilename.substring(originalFilename.lastIndexOf("/") + 1);
				String fileType = fileName.substring(fileName.lastIndexOf(".") + 1);
				String fileSaveName = new Date().getTime() + fileName;
				apkFile = new File(fileUploadPath, fileSaveName);
				// 这里不必处理IO流关闭的问题，因为FileUtils.copyInputStreamToFile()方法内部会自动把用到的IO流关掉
				FileUtils.copyInputStreamToFile(file.getInputStream(), apkFile);
				String md5 = Md5Utils.md5Hex(FileUtils.readFileToByteArray(apkFile));
				String day = DateUtil.getCurrentDateStr();
				fileName = "system/" + day + "/" + md5;
				OssUtils.uploadFile(fileName, file.getInputStream());
				fileVo.setMd5(md5);
				fileVo.setFilePath(fileName);
			} catch (Exception e) {
				LOGGER.error(e.getMessage(), e);
			} finally {
				if (apkFile != null) {
					apkFile.setWritable(true);
					apkFile.delete();
				}
			}
		}
		return fileVo;
	}*/

	@Override
	public void updateSystem(String mark, String isDelete) {
		OS os = new OS();
		os.setMark(mark);
		os.setIsDelete(isDelete);
		systemDao.updateSystem(os);
	}

	@Override
	public OS getByVersion(String version) {
		return systemDao.getByVersion(version);
	}

	@Override
	public void phoneTypeEdit(OS os) {
		systemDao.phoneTypeEdit(os);
	}
	
}
